package com.ruan.controller;

import com.alipay.api.AlipayApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruan.entity.AlipayBean;
import com.ruan.entity.Orders;
import com.ruan.entity.Result;
import com.ruan.query.OrdersQuery;
import com.ruan.service.OrdersService;
import com.ruan.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:OrdersController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/3 22:45
 **/
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Resource
    private PayService payService;

    @PostMapping("/addOrders")
    public Result addOrders(Orders orders){
        return ordersService.addOrders(orders);
    }

    @PostMapping("/getOrdersList")
    public Result getOrdersList(OrdersQuery ordersQuery){
        //开启分页
        PageHelper.startPage(ordersQuery.getPageNum(),ordersQuery.getPageSize());

        List<Orders> ordersList = ordersService.getOrdersList(ordersQuery);

        //当前页，下一页，上一页，首页，尾页。。。。需要一个类进行封装，
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);

        return new Result(200,ordersPageInfo);
    }

    @RequestMapping("/deleteOrders/{id}")
    public Result deleteOrders(@PathVariable Integer id){
        return ordersService.deleteOrders(id);
    }

    @PostMapping("/deleteOrdersByIds")
    public Result deleteOrdersByIds(@RequestParam("ids[]") Integer[] ids){
        return ordersService.deleteOrdersByIds(ids);
    }

    @PostMapping("/getAllOrdersList")
    public Result getAllOrdersList(OrdersQuery ordersQuery){
        //开启分页
        PageHelper.startPage(ordersQuery.getPageNum(),ordersQuery.getPageSize());

        List<Orders> ordersList = ordersService.getAllOrdersList(ordersQuery);

        //当前页，下一页，上一页，首页，尾页。。。。需要一个类进行封装，
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);

        return new Result(200,ordersPageInfo);
    }

    /**
     * 阿里支付
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("/alipay")
    public Map alipay(AlipayBean alipayBean) throws AlipayApiException {
        System.out.println(alipayBean);
        Map<String,Object> map = new HashMap<String,Object>();
        String str = payService.aliPay(alipayBean);
        System.out.println(str);
        map.put("msg",str);
        map.put("code",200);
        return map;
    }

    @RequestMapping("/paySuccess/{id}")
    public Result paySuccess(@PathVariable Integer id){
        return ordersService.paySuccess(id);
    }

}
